package com.iwop.moto.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.dao.MOrderPurchaseDao;
import com.iwop.moto.model.FOrderPurchaseDetail;
import com.iwop.moto.model.MOrderPurchase;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.MOrderPurchaseService;
import com.iwop.moto.service.StoreService;
import com.iwop.moto.utils.PushNoticeUtil;
import com.iwop.moto.utils.stringUtil.BigDecimalUtil;
import com.iwop.moto.utils.stringUtil.ObjectId;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by mobom on 2016/12/9.
 */

@Service
public class MOrderPurchaseServiceImpl implements MOrderPurchaseService {

    @Autowired
    MOrderPurchaseDao mOrderPurchaseDao;

    @Override
    @Transactional
    public String insert(MOrderPurchase mOrderPurchase, String detail) {
        if(mOrderPurchase.getSellerStoreId().equals(mOrderPurchase.getBuyStoreId())){
            return ErrCode.ERROR_ORDER_SEND;
        }
        if(StringUtils.isEmpty(mOrderPurchase.getBuyStoreId())||StringUtils.isEmpty(mOrderPurchase.getSellerStoreId())){
            return ErrCode.ERROR_STORE_INFO;
        }
        MOrderPurchase mo =mOrderPurchaseDao.findOneById(mOrderPurchase.getSid());
        if(mo!=null){
            mOrderPurchase.setSid(SequenceUtil.Serial("Co"));
        }
        double money=0;
        money=addDtail(money,detail,mOrderPurchase.getSid());//详情添加
        if(money==0){
            return ErrCode.ERROR_ORDER_DETAIL_INFO;
        }
        mOrderPurchase.setPrize(0);
        mOrderPurchase.setMoney(money);
        mOrderPurchase.setStatus(0);
        mOrderPurchase.setAddTime(new Date());
        mOrderPurchase.setUpdateTime(new Date());
        mOrderPurchaseDao.insert(mOrderPurchase);
        return ErrCode.SUCCEED;
    }

    @Override
    public String updateOderPurchaseStatus(String oid, int status, String store_id) {
        try {
            MOrderPurchase mOrderPurchase=mOrderPurchaseDao.findOneBySidAndUserId(oid,store_id);
            if(mOrderPurchase.getStatus()!=0){
                return ErrCode.ERROR_UPDATE_ORDER;
            }
            if(mOrderPurchase==null){
                return ErrCode.EXSIT_ORDER_INFO;
            }
            mOrderPurchase.setStatus(status);
            mOrderPurchase.setUpdateTime(new Date());
            mOrderPurchaseDao.update(mOrderPurchase);
        } catch (Exception e) {
            e.printStackTrace();
            return ErrCode.ERROR_SYSTEM;
        }
        return ErrCode.SUCCEED;
    }

    @Override
    public List<MOrderPurchase> findOrderPurchaseList( String seller_store_id, String buy_store_id) {
        List<MOrderPurchase> orderPurchaseByMap = mOrderPurchaseDao.findOrderPurchaseByMap(seller_store_id, buy_store_id);
        List<FOrderPurchaseDetail> purchaseDetail=mOrderPurchaseDao.findPurchaseDetail(seller_store_id, buy_store_id);
        for (MOrderPurchase mOrderPurchase:orderPurchaseByMap){
            List<FOrderPurchaseDetail> list = mOrderPurchase.getList();
            System.out.println("=====");
            Iterator<FOrderPurchaseDetail> it = purchaseDetail.iterator();
            while(it.hasNext()){        //正确做法
                FOrderPurchaseDetail temp = it.next();
                if(temp.getOrderId().equals(mOrderPurchase.getSid())){
                    list.add(temp);
                    it.remove();
                }
            }
        }
        return orderPurchaseByMap;
    }

    @Override
    public MOrderPurchase getPurchaseDetail(String oid) {
        MOrderPurchase orderPurchaseByMap = mOrderPurchaseDao.findOneById(oid);
        if(orderPurchaseByMap==null){
            return new MOrderPurchase();
        }
        List<FOrderPurchaseDetail> purchaseDetail = findPurchaseDetailByOid(oid);
        orderPurchaseByMap.setList(purchaseDetail);
        return orderPurchaseByMap;
    }


    @Override
    public MOrderPurchase findOneById(String oid) {
        MOrderPurchase orderPurchaseByMap = mOrderPurchaseDao.findOneById(oid);
        return orderPurchaseByMap;
    }

    @Override
    public void updateOrderPurchase(MOrderPurchase out_trade_no) {
        mOrderPurchaseDao.update(out_trade_no);
    }

    /**
     * 添加详情
     * */
    private double addDtail(double money,String detail,String orderId){
        List<FOrderPurchaseDetail> fOrderPurchaseDetails = JSONArray.parseArray(detail, FOrderPurchaseDetail.class);
        if(fOrderPurchaseDetails!=null && fOrderPurchaseDetails.size()>0){
            for (FOrderPurchaseDetail fOrderPurchaseDetail:fOrderPurchaseDetails){
                money= BigDecimalUtil.getAdd(money, fOrderPurchaseDetail.getMoney());
                fOrderPurchaseDetail.setSid(ObjectId.UUID());
                fOrderPurchaseDetail.setOrderId(orderId);
                mOrderPurchaseDao.insertDetail(fOrderPurchaseDetail);
            }
            return money;
        }else {
            return 0;
        }
    }

    @Override
    public MOrderPurchase findOneByoutTradeNo(String order_id) {
        return mOrderPurchaseDao.findOneByoutTradeNo(order_id);
    }

    @Override
    public MOrderPurchase findOneByPayOrderNo(String out_trade_no) {
        return mOrderPurchaseDao.findOneByPayOrderNo(out_trade_no);
    }
    @Override
    public MOrderPurchase findOneByOrderId(String payOrderNo) {
        return mOrderPurchaseDao.findOneByOrderId(payOrderNo);
    }

    @Override
    public MOrderPurchase findModerPurchaseByOid(String outTradeNo) {
        return mOrderPurchaseDao.findModerPurchaseByOid(outTradeNo);
    }

    @Override
    public List<FOrderPurchaseDetail> findPurchaseDetailByOid(String sid) {
        List<FOrderPurchaseDetail> purchaseDetail=mOrderPurchaseDao.findPurchaseDetailByOid(sid);
        return purchaseDetail;
    }
}
